package thinking.java.initalization.and.clean.up.example;

/**
 * @author GaoFeng
 * @date 2020/1/2 13:04
 */
public class Ex_5_5_3_BookStatusFinalizeTest {

    /** 书 */
    private String name;
    private boolean status;

    public static void main(String[] args) {

        System.out.println("开始创建Tank对象");

        Tank tank1 = new Tank("Bob", false);
        Tank tank2 = new Tank("Bob", true);
        Tank tank3 = new Tank("Bob", true);
        Tank tank4 = new Tank("Bob", false);

        tank1 = null;
        tank2 = null;
        tank3 = null;
        tank4 = null;

        System.out.println("开始清理没有绑定引用的Tank对象");
        System.gc();
        System.out.println("对象清理完毕");

        // 垃圾回收器是在单独的线程中运行的

    }

    static class Tank {

        String name;
        boolean status;

        public Tank(String name, boolean status) {
            this.name = name;
            this.status = status;
        }

        @Override
        protected void finalize() throws Throwable {
            if (status) {
                System.out.println("clear fail, " + name + " status is full.");
            } else {
                super.finalize();
                System.out.println(this.name + " clear success.");
            }
        }
    }
}
